package com.gin.kotlin.no_gay_alliance.adapter.replyhistory

import android.content.Context
import android.content.DialogInterface
import android.view.View
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.activity.UserCenterActivity
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.AlertDialogUtils
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.database.entity.ReplyHistoryUser
import com.gin.kotlin.no_gay_alliance.databinding.ItemReplyHistoryUserBinding

class ReplyHistoryUserAdapter(context: Context, handler: MyHandler) : AbstractBindingRecyclerViewAdapter<ItemReplyHistoryUserBinding, ReplyHistoryUser>(context, handler,false),
    View.OnClickListener {
    override fun onBindViewHolder(holder: ViewHolder<ItemReplyHistoryUserBinding>, position: Int, b: ItemReplyHistoryUserBinding, dataModel: ReplyHistoryUser) {
        b.tvUsername.text = dataModel.username
        b.tvCount.text = "已记录: ${dataModel.count} 条"

        listOf(b.tvUsername,b.ivDelete).forEach {
            it.tag = dataModel
            it.setOnClickListener(this)
        }
    }

    override fun onClick(v: View?) {
        val replyHistoryUser = v?.tag as? ReplyHistoryUser ?: return
        val index = data.indexOf(replyHistoryUser)
        when (v.id){
            R.id.tv_username-> UserCenterActivity.startActivity(context, replyHistoryUser.userId)
            R.id.iv_delete -> AlertDialogUtils.builder(
                context,
                "移除观察",
                replyHistoryUser.username,
                listener = { _, which -> if (which == DialogInterface.BUTTON_POSITIVE) {
                    App.INSTANCE.database.replyHistoryUserDao().del(replyHistoryUser.userId)
                    removeData(index)
                } }).show()
        }
    }
}